//------------------------------------------------------------------------------
//此代码由工具生成
//请使用partial类进行扩展,避免修改被覆盖
//createTime:2020-11-19 14:14:45
//------------------------------------------------------------------------------
using System.Linq.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
using XKPlatform.DataModels.Dao.BaseDao;
using XKPlatform.DataModels.Dao.BaseEntity;
using XKPlatform.DataModels;
using XKPlatform.IService;
using FreeSql;
using XKPlatform.Common.Enums;
using XKPlatform.DataModels.SysModel;
using XKPlatform.Common.Extends;

namespace XKPlatform.Service
{
    public partial class SysAutoCodeTableCfgService : BaseFreeSqlDao<SysAutoCodeTableCfg>, ISysAutoCodeTableCfgService
    {
        public bool AddModel(SysAutoCodeTableCfg model)
        {
            return InsertAffrows(model) > 0;
        }

        public bool BatchSave(List<SysAutoCodeTableCfg> models)
        {
            return BatchInsert(models) > 0;
        }

        public (List<SysAutoCodeTableCfg> list, long count) GetList(Expression<Func<SysAutoCodeTableCfg, bool>> where, List<SortInfo<SysAutoCodeTableCfg, object>> orderbys = null, PageInfo pageInfo = null)
        {
            return Query(where, orderbys, pageInfo);
        }

        public (List<SysAutoCodeTableCfg> list, long count) GetListByTbName(Expression<Func<SysAutoCodeTableCfg, bool>> where, string tbName, List<SortInfo<SysAutoCodeTableCfg, object>> orderbys = null, PageInfo pageInfo = null)
        {
            throw new NotImplementedException();
        }

        public SysAutoCodeTableCfg GetModel(Expression<Func<SysAutoCodeTableCfg, bool>> where)
        {
            return GetByOne(where);
        }

        public IUpdate<SysAutoCodeTableCfg> UpdateBySets(Expression<Func<SysAutoCodeTableCfg, bool>> where)
        {
            throw new NotImplementedException();
        }

        public bool UpdateModel(SysAutoCodeTableCfg model, Expression<Func<SysAutoCodeTableCfg, bool>> where, Expression<Func<SysAutoCodeTableCfg, object>> IgnoreLmbad = null)
        {
            return Update(model, where, IgnoreLmbad);
        }
        /// <summary>
        /// 根据页面名称增加页面中默认按钮信息
        /// </summary>
        /// <param name="PageName"></param>
        /// <returns></returns>
        public bool SaveButtonInfo(string id, string PageName)
        {
            try
            {
                BaseFreeSqlDao<SysPageButtonInfoCfg> btInfoDao = new BaseFreeSqlDao<SysPageButtonInfoCfg>();
                var btnInfos = btInfoDao.GetList(o => o.TableID == id);//读取页面button 信息

                List<sysApiModel> apiLst = new List<sysApiModel>();

                for (int k = 0; k < btnInfos.Count; k++)
                {
                    var item = btnInfos[k];
                    var btnsDic = EnumExtension.GetEnumDescriptionDic<PageActionEnum>();
                    var btnsTypes = btnsDic[item.ButtonId]?.Split(','); //获取枚举的描述 包含名称和按钮类别
                    sysApiModel apiModel = new sysApiModel();
                    apiModel.ID = Guid.NewGuid().ToString();
                    apiModel.ApiName = btnsTypes[0];
                    apiModel.UrlAddr = item.ActionUrl;
                    apiModel.IsNeedPermisson = item.IsPermission;
                    apiModel.ApiSource = ApiSourceType.sys.ToString();
                    apiModel.description = btnsTypes[1];

                    apiModel.PageUrl = PageName;
                    apiModel.ButtonID = item.ButtonId;
                    apiModel.CreatedOn = DateTime.Now.ToString();
                    apiLst.Add(apiModel);
                }

                //apiModel = new sysApiModel();
                //apiModel.ID = Guid.NewGuid().ToString();
                //apiModel.ApiName = "新增";
                //apiModel.UrlAddr = string.Format("/{0}/SaveOrEdit", PageName);
                //apiModel.IsNeedPermisson = true;
                //apiModel.description = ((int)ButtonTypeEnum.PageBtn).ToString();
                //apiModel.PageUrl = PageName;
                //apiModel.ButtonID = "Add";
                //apiModel.CreatedOn = DateTime.Now.ToString();
                //apiLst.Add(apiModel);

                //apiModel = new sysApiModel();
                //apiModel.ID = Guid.NewGuid().ToString();
                //apiModel.ApiName = "删除";
                //apiModel.UrlAddr = string.Format("/{0}/DelAll", PageName);
                //apiModel.IsNeedPermisson = true;
                //apiModel.description = ((int)ButtonTypeEnum.ColBtn).ToString();
                //apiModel.PageUrl = PageName;
                //apiModel.ButtonID = "del";
                //apiModel.CreatedOn = DateTime.Now.ToString();
                //apiLst.Add(apiModel);

                //apiModel = new sysApiModel();
                //apiModel.ID = Guid.NewGuid().ToString();
                //apiModel.ApiName = "批量删除";
                //apiModel.UrlAddr = string.Format("/{0}/DelAll", PageName);
                //apiModel.IsNeedPermisson = true;
                //apiModel.description = ((int)ButtonTypeEnum.PageBtn).ToString();
                //apiModel.PageUrl = PageName;
                //apiModel.ButtonID = "DelAll";
                //apiModel.CreatedOn = DateTime.Now.ToString();
                //apiLst.Add(apiModel);
                BaseFreeSqlDao<sysApiModel> apiDao = new BaseFreeSqlDao<sysApiModel>();
                apiDao.Delete(o => o.PageUrl == PageName);
                apiDao.BatchInsertAsync(apiLst);

            }
            catch (Exception ex)
            {

            }
            return true;
        }

    }
}
